package com.linln.admin.meeting.service.impl;

import com.linln.admin.meeting.domain.Room;
import com.linln.admin.meeting.repository.RoomRepository;
import com.linln.admin.meeting.service.RoomService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author taofucheng
 * @date 2021/10/25
 */
@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomRepository infoRepository;

    /**
     * 根据ID查询数据
     *
     * @param id 主键ID
     */
    @Override
    public Room getById(Long id) {
        return infoRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     *
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Room> getPageList(Example<Room> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return infoRepository.findAll(example, page);
    }

    /**
     * 保存数据
     *
     * @param info 实体对象
     */
    @Override
    public Room save(Room info) {
        return infoRepository.save(info);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return infoRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    /**
     * 获取指定GroupId的可用会议室
     *
     * @param groupId
     * @return
     */
    @Override
    public List<Room> getByGroupId(String groupId) {
        groupId = StringUtils.trim(groupId);
        if (StringUtils.isBlank(groupId) || !StringUtils.isNumeric(groupId)) {
            return null;
        }
        Long gid = Long.parseLong(groupId);
        return infoRepository.findByGroupIdAndStatus(gid, StatusEnum.OK.getCode().shortValue());
    }

    @Override
    public List<Room> getByStatus(StatusEnum status) {
        if (status == null) {
            status = StatusEnum.OK;
        }
        return infoRepository.findByStatus(status.getCode().shortValue());
    }
}